﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sqllite
{
    public partial class listform : Form
    {
        public listform()
        {
            InitializeComponent();
         

        }

        private void Form1_Load(object sender, EventArgs e)
        {


           
            showData();
        }

      

        private void showData()
        {
            //数据库连接串
            string connStr = "Data Source=47.98.220.131;Initial Catalog=YW_MES;User ID=MES;Password=YW@2222";
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(connStr);
                //打开数据库
                conn.Open();
                string sql = "select * from wzy_edu_taobaoshop";
                String shopname = this.textBox1.Text;
                if (shopname != null && !shopname.Equals(""))
                {
                    sql = sql + " where shopname like '%"+ shopname + "%'";
                }
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
                //设置数据表格为只读
                dataGridView1.ReadOnly = true;
                //不允许添加行
                dataGridView1.AllowUserToAddRows = false;
                //背景为白色
                dataGridView1.BackgroundColor = Color.White;
                //只允许选中单行
                dataGridView1.MultiSelect = false;
                //整行选中
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                //设置数据表格上显示的列标题
                dataGridView1.Columns[0].HeaderText = "商品名";
                dataGridView1.Columns[1].HeaderText = "价格";
                dataGridView1.Columns[2].HeaderText = "购买时间";
                dataGridView1.Columns[3].HeaderText = "是否发货";
                dataGridView1.Columns[4].HeaderText = "是否收货";
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询错误！" + ex.Message);
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
           
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            addform addfrom1 = new addform();
            //弹出修改信息窗口
            DialogResult dr = addfrom1.ShowDialog();
            //判断是否单击确定按钮
            if (dr == DialogResult.OK)
            {
                //调用查询全部课程方法
                showData();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要修改的行！");
            }
            else
            {
                string id = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
                String shopname = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                String price = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                String buydate = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                String fahuo = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                String shouhuo = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                updateform updateform = new updateform(shopname, price, buydate, fahuo, shouhuo,id);
                //弹出修改信息窗口
                DialogResult dr = updateform.ShowDialog();
                //判断是否单击确定按钮
                if (dr == DialogResult.OK)
                {
                    //调用查询全部课程方法
                    showData();
                }
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要删除的行！");
            }
            else
            {
                string id = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();

                string connStr = "Data Source=47.98.220.131;Initial Catalog=YW_MES;User ID=MES;Password=YW@2222";
                SqlConnection conn = null;
                try
                {
                    conn = new SqlConnection(connStr);
                    //打开数据库
                    conn.Open();
                    string sql = "delete from wzy_edu_taobaoshop where id ={0}";
                    sql = String.Format(sql,id);
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    //执行修改操作的SQL
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("删除成功！");
                    //调用查询全部的方法，刷新DataGridView控件中的数据
                    showData();
                }
                catch (Exception  ex )
                {
                    MessageBox.Show("删除失败！" + ex.Message);
                }
                finally
                {
                    if (conn != null)
                    {
                        //关闭数据库连接
                        conn.Close();
                    }
                }
        }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text !=null)
            {
                showData();
            }
           
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
